using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

using Material.Object;
using Material.Entity;


namespace Material.App
{
    public partial class FrmFindBarangDua : Form
    {
        private TblBarang  tblBarang;
        private TblBarangEntity tblBarangEntity;
        private string connStr = ConfigurationManager.ConnectionStrings["dbconnection"].ToString();
        public string idBarang = string.Empty;

        public FrmFindBarangDua()
        {
            InitializeComponent();
            BindGrid();
            cbListParameter.SelectedIndex = 0;

            dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque;
            dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
            dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;

            dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Green;
            dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Yellow;

            //dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            //dataGridView1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //dataGridView1.AllowUserToResizeColumns = false;
        }

        private void FrmFindBarang_Load(object sender, EventArgs e)
        {

        }

        private void BindGrid()
        {
            tblBarangEntity = new TblBarangEntity(connStr);
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = tblBarangEntity.GetFindDataAll().Tables[0];
        }

        private void BindGrid(string parameter,string value)
        {
            tblBarangEntity = new TblBarangEntity(connStr);
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = tblBarangEntity.GetFindDataAll(parameter,value).Tables[0];
        }

        private void cbListParameter_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtFind.Text = string.Empty;
            if (cbListParameter.SelectedIndex == 0)
            {
                txtFind.Visible = false;
                btnFind.Visible = false;
                cbListFind.Visible = false;
                
                //tglAkhir.Visible = false;
                //tglMulai.Visible = false;
                //lblTgl.Visible = false;
                BindGrid();
            }
            else if (cbListParameter.SelectedIndex == 1)
            {
                //cbListFind.Visible = true;
                txtFind.Visible = true;
                btnFind.Visible = true;
                cbListFind.Visible = false;
                //tglAkhir.Visible = false;
                //tglMulai.Visible = false;
                //lblTgl.Visible = false;

                txtFind.Focus();
            }
            else if (cbListParameter.SelectedIndex == 2)
            {
                cbListFind.Visible = true;
                txtFind.Visible = false;
                btnFind.Visible = true;

                //tglAkhir.Visible = false;
                //tglMulai.Visible = false;
                //lblTgl.Visible = false;



                TblKelompokEntity tblKelompokEntity = new TblKelompokEntity(connStr);
                cbListFind.DataSource = tblKelompokEntity.GetDataAll();
                cbListFind.ValueMember = "Id";
                cbListFind.DisplayMember = "Kelompok";
            }
            else if (cbListParameter.SelectedIndex == 3)
            {
                cbListFind.Visible = true;
                txtFind.Visible = false;
                btnFind.Visible = true;

                //tglAkhir.Visible = false;
                //tglMulai.Visible = false;
                //lblTgl.Visible = false;


                TblSatuanEntity tblSatuanEntity = new TblSatuanEntity(connStr);
                cbListFind.DataSource = tblSatuanEntity.GetDataAll();
                cbListFind.ValueMember = "Id";
                cbListFind.DisplayMember = "Satuan";

            }
            //else if (cbListParameter.SelectedIndex == 4)
            //{
            //    //tglAkhir.Visible = true;
            //    //tglMulai.Visible = true;
            //    //lblTgl.Visible = true;

            //    cbListFind.Visible = false;
            //    txtFind.Visible = false;
            //    btnFind.Visible = true;

            //}
            
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            if (cbListParameter.SelectedIndex == 1)
            {
                if (txtFind.Text.Trim().Length == 0)
                {
                    MessageBox.Show("Parameter pencarian harus diisi..","Pesan",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                    return;
                }
                else
                {
                    BindGrid("NamaBarang",txtFind.Text.Trim());
                }
            }

            if (cbListParameter.SelectedIndex == 2)
                BindGrid("KelompokBarang",cbListFind.Text);

            if (cbListParameter.SelectedIndex == 3)
                BindGrid("SatuanBarang", cbListFind.Text);

            //if (cbListParameter.SelectedIndex ==4)
            //    BindGrid("TglBeli", tglMulai.Value.ToString("dd/MM/yyy") + "|" + tglAkhir.Value.ToString("dd/MM/yyyy"));

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {
            
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if ((e.RowIndex > -1) && (e.ColumnIndex > -1))
            {

                switch (e.ColumnIndex)
                {
                    case 2:
                        //FrmPenjBarangDTL.IdTransDTL = Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
                        //FrmPenjBarangDTL.IdBarang = Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString());
                        idBarang = (dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString());
                        this.Close();
                        break;
                }
            }
        }

    }
}